<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%@include file="/common/common.jsp" %>
    <link rel="stylesheet" href="${basePath}/css/layerPage.css${VERSION}"/>
    <link rel="stylesheet" href="${basePath}/assets/css/foundation-datepicker.css"/>
	<script type="text/javascript" src="${basePath}/assets/js/foundation-datepicker.js"></script>
</head>
<body>
<form id="form" name="form" class="form-horizontal" method="post" action="${basePath}/userTask/addUserTask">
    <div class="panel-body">
        <div class="form-group" id="titlediv">
            <label class="col-sm-3 control-label">任务名称：</label>
            <div class="col-sm-9">
                <input type="text" class="col-sm-5" id="title" name="title"/>
                <input type="hidden" name="taskId" id="taskId"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">分配人员：</label>
            <div class="col-sm-9">
                <input class="col-sm-5" type="text" name="userName" id="userName" readonly="true"/>
                <input type="hidden" name="alloterId" id="alloterId"/>
                <input type="hidden" name="alloterIds" id="alloterIds"/>
                <button type="button" id="chooseUserBtn" class="btn btn-danger">选择人员</button>
            </div>
        </div>
        
	        <!--  div class="form-group">
	            <label class="col-sm-3 control-label">计划开始时间：</label>
	            <div class="col-sm-9">
	                <input type="text" class="date-icon" id="startTimePl" name="startTimePl" readonly/>
	            </div>
	            
        	</div>
        	<div class="form-group">
	            <label class="col-sm-3 control-label"></label>
	            <div class="col-sm-9">
	                <button type="button" id="addDays" class="btn btn-danger">+</button>
	                <button type="button" id="delDays" class="btn btn-danger">-</button>
	                <button type="button" id="tomorrows" class="btn btn-danger">明天</button>
	                <button type="button" id="acquireds" class="btn btn-danger">后天</button>
	            </div>
	        </div>
	
	        <div class="form-group">
	            <label class="col-sm-3 control-label">计划结束时间：</label>
	            <div class="col-sm-9">
	                <input type="text" class="date-icon" id="endTimePl" name="endTimePl" readonly/>
	            </div>
	        </div>
	        <div class="form-group">
	            <label class="col-sm-3 control-label"></label>
	            <div class="col-sm-9">
	                <button type="button" id="addDaye" class="btn btn-danger">+</button>
	                <button type="button" id="delDaye" class="btn btn-danger">-</button>
	                <button type="button" id="tomorrowe" class="btn btn-danger">明天</button>
	                <button type="button" id="acquirede" class="btn btn-danger">后天</button>
	            </div>
       		</div>
	        <div class="form-group">
	            <label class="col-sm-3 control-label">描述：</label>
	            <div class="col-sm-9">
	                <textarea id="userTaskDes" name="userTaskDes" rows="5" cols="30"></textarea>
	            </div>
	        </div>
       -->
        <div class="form-group" id="submitdiv">
        	<label class="col-sm-3 control-label"></label>
        	<button id="submitdiv" type="submit" class="btn btn-primary btn-submit">提交</button>
        </div>
        <div class="form-group" id="submitdiv">
        	<label class="col-sm-3 control-label"></label>
        	<button id="clickdiv" type="button" class="btn btn-primary btn-submit">确认</button>
        </div>
        
    </div>
</form>
<script type="text/javascript">

	//alert(parent.taskTitle+"--------");
	
	$("#title").val(parent.taskTitle);
	$("#taskId").val(parent.taskId);
	$("#clickdiv").hide();
	var first = parent.first;
	//alert(parent.$("#title").val());
	if(first==1){
		$("#clickdiv").show();
		$("#titlediv").hide();
		$("#submitdiv").hide();
	}
	
    jQuery(function ($) {
    	
    	
    	//日期处理开始
    	var mydate = new Date();
    	var str = "" + mydate.getFullYear() + "-";
    	str += ((mydate.getMonth()+1)<10?'0'+(mydate.getMonth()+1):(mydate.getMonth()+1)) + "-";
    	str += (mydate.getDate())<10?'0'+(mydate.getDate()):(mydate.getDate());
    	$("#startTimePl").val(str);
    	$("#endTimePl").val(str);
    	
    	
    	$("#addDays").click(function() {
			$("#startTimePl").val(change_date(1,1));
		});

		$("#delDays").click(function() {
			$("#startTimePl").val(change_date(-1,1));
		});
			
			
		$("#addDaye").click(function() {
			$("#endTimePl").val(change_date(1,0));
		});
		
		$("#delDaye").click(function() {
			$("#endTimePl").val(change_date(-1,0));
		});	
    	
    	$("#tomorrowe").click(function() {
			$("#endTimePl").val(change_date(1,2));
		});	
		
		$("#acquirede").click(function() {
			$("#endTimePl").val(change_date(2,2));
		});	
		
		$("#tomorrows").click(function() {
			$("#startTimePl").val(change_date(1,2));
		});	
		
		$("#acquireds").click(function() {
			$("#startTimePl").val(change_date(2,2));
		});	
		
		
		
		var change_date = function(days,typ) {
			// 参数表示在当前日期下要增加的天数
			var now;
			if(typ==1){
				now = new Date($("#startTimePl").val());
			}else if(typ==0){
				now = new Date($("#endTimePl").val());
			}else{
				now = new Date();
			}

			// + 1 代表日期加，- 1代表日期减
			now.setDate((now.getDate() + days));
			
			var year = now.getFullYear();
			var month = now.getMonth() + 1;
			var day = now.getDate();
			if (month < 10) {
				month = '0' + month;
			}
			if (day < 10) {
				day = '0' + day;
			}

			return year + '-' + month + '-' + day;
		};
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	jQuery.validator.methods.compareDate = function(value, element, param) {
            var startDate = jQuery(param).val();
            //alert(param+"--"+value);
            var date1 = new Date(Date.parse(startDate.replace("-", "/")));
            var date2 = new Date(Date.parse(value.replace("-", "/")));
            return date1 <= date2;
        };
    	
    	
    	$('.date-icon').fdatepicker({
            format: 'yyyy-mm-dd'
        });
    	
        var $form = $("#form");
        var $title = $("#title");
        var $userName = $("#userName");
        var $alloterId = $("alloterId");
        var $startTimePl = $("#startTimePl");
        var $endTimePl = $("#endTimePl");
        var $userTaskDes = $("#userTaskDes");
        var $submit = $("#submit");

        $form.validate({
        	errorClass: 'error-msg',
            rules: {
            	title:{
                    required:true/*,
                    remote: {
                        type: "post",
                        url: "${basePath}/task/checkTitle",
                        data: {
                        	title: function() {
                                return $("#title").val();
                            }
                        },
                        dataType: "json",
                        dataFilter: function(data, type) {
                            var json = eval('('+data+')');
                            if(json.status == '0'){
                                return true;
                            } else {
                                return false;
                            }
                        }
                    }*/
                },
                startTimePl: {
                    required: true
                },
                endTimePl: {
                    required: true,
                    compareDate: "#startTimePl"
                },
                userTaskDes:{
                	rangelength:[0,100]
                } 

            },
            messages: {
            	title: {
                    required: "标题必填*"
                },
                startTimePl: "计划开始时间必填*",
                endTimePl:{
                	required:"计划结束时间必填*",
                	compareDate:"计划结束日期必须大于计划开始日期!"
                },
                userTaskDes:"任务说明长度不能超过100！*"
            },
            submitHandler: function (form) {
            	
            	var alloterId = $("#alloterId").val()
            	
            	if(alloterIds==''){
            		layer.alert('分配人不能为空！');
            		return;
            	}
            	/*
            	var start = $("#startTimePl").val();
            	var start1 = parent.startTimePl;
            	//alert(start+'-'+start1);
            	var date1 = new Date(Date.parse(start.replace("-", "/")));
                var date2 = new Date(Date.parse(start1.replace("-", "/")));
                
                if(date1<date2){
            		layer.alert('分配人的任务计划开始时间应大于等于任务的计划开始时间！');
            		return;
            	}
            	var end = $("#endTimePl").val();
            	var end1 = parent.endTimePl;
            	alert(end+'-'+end1);
            	date1 = new Date(Date.parse(end.replace("-", "/")));
                date2 = new Date(Date.parse(end1.replace("-", "/")));
                
            	if(date1>date2){
            		layer.alert('分配人的任务计划结束时间应小于等于任务的计划结束时间！');
            		return;
            	}
            	*/
            	
                $.ajax({
                    url: $form.attr("action"),
                    type: $form.attr("method"),
                    data: $form.serialize(),
                    
                    dataType: "json",
                    cache: false,
                    beforeSend: function () {
                        $submit.prop("disabled", true);
                    },
                    success: function (data) {
                    	//alert(data.status);
                        if (data.status == '0') {
                            $submit.prop("disabled", false);
                            layer.confirm('任务分配成功！是否关闭窗口？', {icon: 1}, function (index) {
                                parent.$("#grid-table").jqGrid().trigger("reloadGrid", [{page: 1}]);
                                parent.layer.close(parent.pageii);
                                return false
                            });
                        } else {
                            $submit.prop("disabled", false);
                            layer.alert(data, {
                                icon: 0,
                                skin: 'layui-layer-lan',
                                closeBtn: 0
                            });
                        }
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        $submit.prop("disabled", false);
                        layer.alert("系统未知错误", {
                            icon: 0,
                            skin: 'layui-layer-lan',
                            closeBtn: 0
                        });
                    }
                });
			}
        });

        
        $('#chooseUserBtn').click("click", function () {
            openOrg();
        });

        //选择部门
        function openOrg() {       	
            var pageii = layer.open({
                title: false,
                type: 2,
                area: ["800px", "600px"],
                content: ['${basePath}/task/anon/toOrgTree']//,
                //btn: ["确认", "关闭"],
                //yes: function (index, layero) {
                	//var userName = $(layero).find("iframe")[0].contentWindow.userName;
                    //var alloterId = $(layero).find("iframe")[0].contentWindow.alloterId;
                    //layer.close(index);
                    //$("#userName").val(userName);
                    //$("#alloterId").val(alloterId);
                //}, cancel: function () {
                   // return true;
                //}
            });
        }
        $('#clickdiv').click("click", function () {
        	confirm();
        });
        function confirm() {
        	var start = $("#startTimePl").val();
        	var start1 = parent.$("#startTimePl").val();
        	var alloterId = $("#alloterId").val()
        	
        	if(alloterIds==''){
        		layer.alert('分配人不能为空！');
        		return;
        	}
			
        	if(start==''){
        		layer.alert('分配人的任务计划开始时间不应为空！');
        		return;
        	}
        	var date1 = new Date(Date.parse(start.replace("-", "/")));
            var date2 = new Date(Date.parse(start1.replace("-", "/")));
        	if(date1<date2){
        		layer.alert('分配人的任务计划开始时间应大于等于任务的计划开始时间！');
        		return;
        	}
        	var end = $("#endTimePl").val();
        	if(end==''){
        		layer.alert('分配人的任务计划结束时间不应为空！');
        		return;
        	}
        	
        	var end1 = parent.$("#endTimePl").val();
        	date1 = new Date(Date.parse(end.replace("-", "/")));
            date2 = new Date(Date.parse(end1.replace("-", "/")));
            
        	if(date1>date2){
        		layer.alert('分配人的任务计划结束时间应小于等于任务的计划结束时间！');
        		return;
        	}
        	
        	
      		parent.$("#alloterId").val($("#alloterId").val());
          	parent.$("#aStartPl").val($("#startTimePl").val());
          	parent.$("#aEndPl").val($("#endTimePl").val());
          	parent.$("#aDesc").val($("#userTaskDes").val());
          	parent.$("#userName").val($("#userName").val());
          	
          	
          	
          	layer.confirm('任务添加成功！是否关闭窗口？', {icon: 1}, function (index) {
                  parent.layer.close(parent.pageii);
                  return false;
              }); 
        }
    });
</script>
</body>
</html>
